﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.CoreDomain;
using Actuals.Business;
using Actuals.Common;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;



namespace Actuals
{
    public partial class ShowEmployeeLeaves : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IBusinessTeam businessTeam = new BusinessTeam();
                IList<ITeam> teams = businessTeam.GetTeams();
                ddlTeam.DataSource = teams;
                ddlTeam.DataTextField = "TeamDescription";
                ddlTeam.DataValueField = "TeamId";
                ddlTeam.DataBind();
                ddlTeam.Items.Insert(0, "Select");




            }

        }

        protected void ddlTeam_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            IBusinessTeamMember businessTeamMember = new BusinessTeamMember();
            IList<ITeamMember> teamMemebers = businessTeamMember.GetTeamMembers(int.Parse(ddlTeam.SelectedValue));
            List<ITeamMember> memeberName = new List<ITeamMember>();
            IList<IEmployee> EmployeeNames = new List<IEmployee>();
            foreach (ITeamMember memberName in teamMemebers)
            {

                IBusinessEmployee businessEmployee = new BusinessEmployee();
                IEmployee employee = businessEmployee.GetEmployee(memberName.EmployeeId);
                EmployeeNames.Add(employee);

            }
            ddlTeamMemeber.DataSource = EmployeeNames;
            ddlTeamMemeber.DataTextField = "NickName";
            ddlTeamMemeber.DataValueField = "Id";
            ddlTeamMemeber.DataBind();
            ddlTeamMemeber.Items.Insert(0, "Select");
            ddlTeamMemeber.Items.Insert(1, "All");
        }

        protected void BindGridView()
        {

            IBusinessEmployeeLeave businessEmployeeLeave = new BusinessEmployeeLeave();
            IList<IEmployeeLeave> employeeLeaves;
            if (DropDownList1.SelectedIndex == 0)
            {
                if (ddlTeamMemeber.Text == "All")
                {
                    int teamId = int.Parse(ddlTeam.SelectedValue);

                    DateTime fromDate;
                    string[] _date = txtFromDate.Text.Split('-');

                    fromDate = new DateTime(int.Parse(_date[2]), int.Parse(_date[0]), int.Parse(_date[1]));

                    DateTime toDate;
                    string[] _date1 = txtToDate.Text.Split('-');

                    toDate = new DateTime(int.Parse(_date1[2]), int.Parse(_date1[0]), int.Parse(_date1[1]));
                    employeeLeaves = businessEmployeeLeave.GetEmployeeLeavesForTeam(teamId, fromDate, toDate);

                }
                else
                {
                    int empId = int.Parse(ddlTeamMemeber.SelectedValue);
                    employeeLeaves = businessEmployeeLeave.GetEmployeeLeaves(empId);

                }
            }
            else
            {
                if (ddlTeamMemeber.Text == "All")
                {
                    int teamId = int.Parse(ddlTeam.SelectedValue);
                    employeeLeaves = businessEmployeeLeave.GetEmployeeLeavesForTeam(teamId);

                }
                else
                {
                    int empId = int.Parse(ddlTeamMemeber.SelectedValue);
                    employeeLeaves = businessEmployeeLeave.GetEmployeeLeaves(empId);

                }
            }
            gvResults.DataSource = employeeLeaves;
            gvResults.DataBind();
        }

        protected void btnEmployeeLeaves_Click(object sender, EventArgs e)
        {
            BindGridView();

        }

        protected string getEmpName(Object empId)
        {
            IBusinessEmployee businessEmployee = new BusinessEmployee();
            IEmployee employeeName = businessEmployee.GetEmployee(int.Parse(empId.ToString()));
            return employeeName.NickName;
        }

        protected string getDateFormat(object leaveDate)
        {
            return Convert.ToDateTime(leaveDate).ToString("dd/MM/yy");
        }
        protected string getLeaveType(object leaveType)
        {
            IBusinessLeaveType businessLeaveType = new BusinessLeaveType();
            string leaveTypeName = businessLeaveType.getLeaveTypeName(leaveType.ToString());
            return leaveTypeName;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvResults.Rows.Count; i++)
                {
                    CheckBox cb = (CheckBox)gvResults.Rows[i].FindControl("chkShowEmployeeLeave");
                    Label lbl = (Label)gvResults.Rows[i].FindControl("lblEmpId");
                    if (cb.Checked)
                    {
                        string empId = string.Empty;

                        if (lbl != null)
                        {
                            empId = lbl.Text;
                            IBusinessEmployeeLeave businessEmployeeLeave = new BusinessEmployeeLeave();
                            businessEmployeeLeave.DeleteEmployeeLeave(int.Parse(empId.ToString()));
                        }
                    }


                }
                lblMsg.Text = "Deleted";
                BindGridView();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }


    }
}


